package com.youmu.sichuansfc.controller;

import com.youmu.sichuansfc.ano.FundManager.FundUserAuth;
import com.youmu.sichuansfc.ano.FundManager.FundUserTokenUser;
import com.youmu.sichuansfc.common.CommonRes;
import com.youmu.sichuansfc.domain.YoumuFundUser;
import com.youmu.sichuansfc.domain.YoumuNotify;
import com.youmu.sichuansfc.dto.PageT;
import com.youmu.sichuansfc.dto.req.NotifyUploadDto;
import com.youmu.sichuansfc.service.NotifyService;
import com.youmu.sichuansfc.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 *  布告
 * @author WYZ
 * @create 2018-01-31 17:59
 * @desc
 **/
@Controller
public class NotifyController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    NotifyService notifyService;
    /**
     *  发布 修改 通知
     * @param notifyUploadDto
     * @return
     */
    @RequestMapping( value = "/notify/upload", method = RequestMethod.POST)
    public  @ResponseBody
    CommonRes uploadNotify(@RequestBody NotifyUploadDto notifyUploadDto){

        YoumuNotify youmuNotify = new YoumuNotify();
        BeanUtils.copyProperties(notifyUploadDto, youmuNotify);
        youmuNotify.setAddTime(new Date());
        if(youmuNotify.getIsDelete() == null)
            youmuNotify.setIsDelete(false);
        try {
            notifyService.upload(youmuNotify);
            return  CommonRes.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }


    /**
     * 前台布告阅读
     * @param notifyId
     * @param youmuFundUser
     * @return
     */
    @RequestMapping( value = "/fund/notify/read.bs", method = RequestMethod.GET)
    @FundUserAuth
    public @ResponseBody CommonRes readNotify(@RequestParam(name = "id") Long notifyId,
                                              @FundUserTokenUser YoumuFundUser youmuFundUser){
        try {
            notifyService.readNotify(notifyId, youmuFundUser.getId());
            return  CommonRes.success(notifyService.findById(notifyId));
        }catch (Exception e){
            e.printStackTrace();
        }
        return CommonRes.fail();
    }


    /**
     * 后台拉取 布告列表
     * @param startTime
     * @param endTime
     * @param title
     * @param num
     * @param size
     * @param type 类型 : 0 通告; 1行业； 2法律
     * @return
     */
    @RequestMapping( value = "/fund/notify/{type}/search/", method = RequestMethod.GET)
    public @ResponseBody CommonRes notifySearch(@RequestParam(name = "startTime", required = false ) String startTime,
                                    @RequestParam(name = "endTime", required = false) String endTime,
                                    @RequestParam(name = "title", required = false) String title,
                                    @RequestParam(name = "num", required = false, defaultValue = "0") int  num,
                                    @RequestParam(name = "size", required = false, defaultValue = "10") int size,
                                    @PathVariable(name="type") int type ){
        logger.info("------------------------------------------");

        logger.info("startTime = " + startTime + "  endTime = "+ endTime);

        Date _startTime = DateUtil.Date_parseStr(startTime, "YYYY-MM-HH");
        Date _endTime = DateUtil.Date_parseStr(endTime, "YYYY-MM-HH");
        logger.info("startTime = " + _startTime + "  endTime = "+ _endTime);
        logger.info("------------------------------------------");
        try {
            PageT pageT = notifyService.findBy(_startTime, _endTime, title, num, size, type);
            return  CommonRes.success(pageT);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }

    /**
     * 前台拉取 布告列表
     * @param isRank  是否已读
     * @param title
     * @param num
     * @param size
     * @param youmuFundUser
     * @return
     */
    @RequestMapping( value = "/fund/notify/{type}/search.bs", method = RequestMethod.GET)
    @FundUserAuth
    public @ResponseBody CommonRes notifySearch(
                    @RequestParam(name = "title", required = false) String title,
                    @RequestParam(name = "rank", required = false) String rank,
                                     @RequestParam(name = "num", required = false, defaultValue = "0") int  num,
                                     @RequestParam(name = "size", required = false, defaultValue = "10") int size,
                                     @FundUserTokenUser YoumuFundUser youmuFundUser,
                                     @PathVariable(name="type") int type){
        Boolean rank2 =  StringUtils.isNoneEmpty(rank) ? (!"0".equals(rank) ? true : false) : null;
        try {
            PageT pageT = notifyService.findBy(rank2, title,  youmuFundUser.getId(), num, size, type);
            return  CommonRes.success(pageT);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }

    /**
     * 后台通知详情
     * @param id
     * @return
     */
    @RequestMapping( value = "/fund/notify/detail", method = RequestMethod.GET)
    public  @ResponseBody CommonRes notifyDetail(@RequestParam(name = "id") Long id){
        try {
            return  CommonRes.success(notifyService.findById(id));
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }

    /**
     *  最热通知
     * @param num
     * @param size
     * @param type
     * @return
     */
    @RequestMapping( value = "/fund/notify/{type}/search/re", method = RequestMethod.GET)
    public @ResponseBody CommonRes notifyRe(@RequestParam(name = "num", required = false, defaultValue = "0") int  num,
                                            @RequestParam(name = "size", required = false, defaultValue = "10") int size,
                                            @PathVariable(name="type") int type ){
        try {
            PageT pageT = notifyService.findByFilter( num, size, type);
            return  CommonRes.success(pageT);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }
}
